package com.itqf.controller;

import com.itqf.annotation.RequiresPermissionsDesc;
import com.itqf.bean.DtsAdmin;
import com.itqf.dto.DtsSearchDto;
import com.itqf.service.DtsAdminService;
import com.itqf.util.ResponseUtil;
import com.itqf.utils.AdminResponseCode;
import com.itqf.utils.AdminResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * projectName: sm_project
 *
 * @author: 赵伟风
 * time: 2021/7/15 9:42
 * description:管理员模块的接口
 */

@RestController
@CrossOrigin("*")
@RequestMapping("/admin/admin")
public class AdminAdminController {

    @Autowired
    private DtsAdminService dtsAdminService;

    public Logger logger = LoggerFactory.getLogger(AdminAdminController.class);
    /**
     * 权限规则  admin:模块:动作
     * RequiresPermissionsDesc 权限展示
     *    menu只能写两层 权限的前两层
     * @param dto
     * @return
     */
    @RequiresPermissions("admin:admin:list")
    @RequiresPermissionsDesc(menu = {"系统管理","管理员"},button = "列表展示")
    @GetMapping("list")
    public Object list(DtsSearchDto dto){

        logger.debug("[管理员列表]管理员列表查询参数:{}", dto);

        Map data = dtsAdminService.findAdminByPage(dto);

        logger.debug("[管理员列表]分页查询数据为:{}", data);

        return ResponseUtil.ok(data);
    }


    /**
     * 4. 数据需不需要???验证数据库中是否已经存在账号!
     *
     * 2. 前端给我们的数据是完整的么
     * 3. 不完整,我们补全插入
       1.数据库插入操作 insert

     * 5. 做回显
     * @param admin
     * @return
     */
    @RequiresPermissions("admin:admin:create")
    @RequiresPermissionsDesc(menu = {"系统管理","管理员"},button= "管理员添加")
    @PostMapping("create")
    public Object create(@RequestBody DtsAdmin admin){

        //1.用户名是否数据库存在判断
        String username = admin.getUsername();

        if (username == null || username == "")
        {
            logger.warn("[保存管理员]接收到的用户名为null,无法注册!");
            return ResponseUtil.fail();
        }

        boolean hasUserName = dtsAdminService.checkUserName(username);

        if (hasUserName)
        {
            logger.error("[保存管理员]用户名:{},已经被注册!",username);

            return AdminResponseUtil.fail(AdminResponseCode.ADMIN_NAME_EXIST);
        }

        //2.保存用户的业务
        Md5Hash md5Hash = new Md5Hash(admin.getPassword(), "store", 2);
        admin.setPassword(md5Hash.toString());

        admin = dtsAdminService.saveAdmin(admin);

        //4.返回结果即可

        logger.info("[保存管理员]接口调用结束!获取回显数据为:{}",admin);

        return ResponseUtil.ok(admin);
    }



    @RequiresPermissions("admin:admin:update")
    @RequiresPermissionsDesc(menu = {"系统管理","管理员"},button="管理员修改")
    @PostMapping("update")
    public Object update(@RequestBody DtsAdmin dtsAdmin)
    {
        //1.完善数据
        dtsAdmin.setPassword(new Md5Hash(dtsAdmin.getPassword(), "store", 2).toString());

        //2.修改数据
        int rows = dtsAdminService.updateAdmin(dtsAdmin);
        //3.返回结果

        if (rows== 0) {
            return ResponseUtil.fail();
        }

        return ResponseUtil.ok(dtsAdmin);
    }


    @RequiresPermissions("admin:admin:delete")
    @RequiresPermissionsDesc(menu = {"系统管理","管理员"},button="管理员删除")
    @PostMapping("delete")
    public Object delete(@RequestBody DtsAdmin dtsAdmin){

        logger.debug("[删除管理员]传入的id为:{}", dtsAdmin.getId());

        int rows = dtsAdminService.removeAdminById(dtsAdmin.getId());

        if (rows == 0) {

            return ResponseUtil.fail();
        }

        logger.info("[删除管理员]删除影响行数为:{}",rows);

        return ResponseUtil.ok();
    }




}
